#pragma once
#include<iostream>
#include<algorithm>
#include<assert.h>
#include<list>
#include "reverse_iterator.h"
//#include "iterator.h"
using namespace std;
namespace MyList
{
	template<class T>
	struct list_node
	{
		T _data; //存储数据
		list_node<T>* _next; //存储下一个节点的指针
		list_node<T>* _prev; //存储上一个节点的指针
		list_node(const T& data = T())
			:_data(data),
			_next(nullptr),
			_prev(nullptr)
		{
		}
	};

	template<class T, class Ref, class Ptr>
	struct list_iterator
	{
		typedef list_node<T> Node; //节点
		typedef list_iterator<T, Ref, Ptr> Self; //迭代器对象
		typedef typename Ref Ref;
		typedef typename Ptr Ptr;
		list_iterator(Node* n)
			:node(n)
		{ }
		/*list_iterator(const Self& it)
		{
			node = it.node;
		}*/
		Self& operator++() //前置++
		{
			node = node->_next;
			return *this;
		}
		Self& operator--() //前置--
		{
			node = node->_prev;
			return *this;
		}
		Self operator++(int) //后置++
		{
			Self tmp(*this);
			node = node->_next;
			return tmp;
		}
		Self operator--(int) //后置--
		{
			Self tmp(*this);
			node = node->_prev;
			return tmp;
		}
		bool operator!=(const Self& it)
		{
			return node != it.node;
		}
		bool operator==(const Self& it)
		{
			return node == it.node;
		}
		Ref operator*() const
		{
			return node->_data;
		}
		Ptr operator->() const
		{
			return &(node->_data);
		}
		Node* node; //存储节点指针
	};

	template<class T>
	class list
	{
	public:
		typedef list_node<T> Node; //节点
		typedef list_iterator<T, T&, T*> iterator; //普通迭代器
		typedef list_iterator<T, const T&, const T*> const_iterator; //const迭代器
		//typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
		typedef ReverseIterator<iterator> reverse_iterator;
		//typedef ReverseIterator<const_iterator, const T&, const T*> const_reverse_iterator;
		typedef ReverseIterator<const_iterator> const_reverse_iterator;

		iterator begin()
		{
			return iterator(phead->_next);
		}
		iterator end()
		{
			return iterator(phead);
		}
		const_iterator begin() const
		{
			return const_iterator(phead->_next);
		}
		const_iterator end() const
		{
			return const_iterator(phead);
		}
		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}
		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}
		const_reverse_iterator rbegin() const
		{
			return const_reverse_iterator(end());
		}
		const_reverse_iterator rend() const
		{
			return const_reverse_iterator(begin());
		}
		bool empty() const
		{
			return phead->_next == phead;
		}
		size_t size() const
		{
			return _size;
		}
		void empty_list()
		{
			phead = new Node();
			phead->_next = phead;
			phead->_prev = phead;
		}
		list()
			:_size(0)
		{
			empty_list();
		}
		/*list(initializer_list<T> lt)
			:_size(0)
		{
			empty_list();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}*/
		list(initializer_list<T> lt)
			:_size(0)
		{
			empty_list();
			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}
		/*list(const list<T>& lt)
			:_size(0)
		{
			empty_list();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}*/
		list(const list<T>& lt)
			:_size(0)
		{
			empty_list();
			list<T> tmp(lt.begin(), lt.end());
			swap(tmp);
		}
		list(size_t n, const T& val = T())
			:_size(0)
		{
			empty_list();
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		list(int n, const T& val = T())
			:_size(0)
		{
			empty_list();
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}
		template <class InputIterator>
		list(InputIterator first, InputIterator last)
			:_size(0)
		{
			empty_list();
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		/*void resize(size_t n, const T& val = T())
		{
			if (n < _size)
			{
				size_t len = _size - n;
				iterator it = end();
				for (size_t i = 0; i < len; i++)
				{
					it = erase(--it);
				}
			}
			else {
				size_t len = n - _size;
				for (size_t i = 0; i < len; i++)
				{
					push_back(val);
				}
			}
		}*/
		void resize(size_t n, const T& val = T())
		{
			if (n < _size)
			{
				size_t len = _size - n;
				for (size_t i = 0; i < len; i++)
				{
					pop_back();
				}
			}
			else {
				size_t len = n - _size;
				for (size_t i = 0; i < len; i++)
				{
					push_back(val);
				}
			}
		}
		//list<T>& operator=(const list<T>& lt)
		//{
		//	if (this != &lt) //防止自己给自己赋值
		//	{
		//		clear(); //清空链表的有效结点
		//		for (auto& e : lt) //将lt的每个元素插入到当前链表的末尾
		//		{
		//			push_back(e);
		//		}
		//	}
		//	return *this; //返回当前对象的引用
		//}
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}
		void swap(list<T>& lt)
		{
			std::swap(phead, lt.phead);
			std::swap(_size, lt._size);
		}
		~list()
		{
			clear();
			delete phead;
			phead = nullptr;
			_size = 0;
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it); //迭代器it失效后，it指向下一个结点
				it++;
			}
			phead->_next = phead;
			phead->_prev = phead;
			_size = 0;
		}
		void push_back(const T& val)
		{
			/*Node* newnode = new Node(x);
			Node* tail = phead->_prev;
			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = phead;
			phead->_prev = newnode;*/

			insert(end(), val);
		}
		iterator insert(iterator position, const T& val)
		{
			Node* cur = position.node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(val);

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
			++_size;
			return iterator(newnode);
		}
		void insert(iterator position, size_t n, const T& val)
		{
			Node* cur = position.node;
			Node* prev = cur->_prev;
			for (size_t i = 0; i < n; i++)
			{
				Node* newnode = new Node(val);
				prev->_next = newnode;
				newnode->_prev = prev;
				prev = newnode;
			}
			prev->_next = cur;
			cur->_prev = prev;
			_size += n;
		}
		void insert(iterator position, int n, const T& val)
		{
			Node* cur = position.node;
			Node* prev = cur->_prev;
			for (int i = 0; i < n; i++)
			{
				Node* newnode = new Node(val);
				prev->_next = newnode;
				newnode->_prev = prev;
				prev = newnode;
			}
			prev->_next = cur;
			cur->_prev = prev;
			_size += n;
		}
		template <class InputIterator>
		void insert(iterator position, InputIterator first, InputIterator last)
		{
			Node* cur = position.node;
			Node* prev = cur->_prev;
			while (first != last)
			{
				Node* newnode = new Node(*first);
				prev->_next = newnode;
				newnode->_prev = prev;
				prev = newnode;
				++_size;
				++first;
			}
			prev->_next = cur;
			cur->_prev = prev;
		}
		iterator erase(iterator position)
		{
			assert(!empty());
			assert(position != end());
			Node* del = position.node;
			Node* prev = del->_prev;
			Node* cur = del->_next;

			prev->_next = cur;
			cur->_prev = prev;
			delete del;
			del = nullptr;
			--_size;
			return iterator(cur);
		}
		iterator erase(iterator first, iterator last)
		{
			assert(!empty());
			assert(first != end());
			Node* prev = (first.node)->_prev;
			Node* next = last.node;
			while (first != last)
			{
				iterator tmp(first++);
				delete tmp.node;
				--_size;
			}
			prev->_next = next;
			next->_prev = prev;
			return last;
		}
		void push_front(const T& val)
		{
			insert(begin(), val);
		}
		void pop_front()
		{
			assert(!empty());
			erase(begin());
		}
		void pop_back()
		{
			assert(!empty());
			erase(--end());
		}
		T& front()
		{
			return *begin();
		}
		const T& front() const
		{
			return *begin();
		}
		T& back()
		{
			return *(--end());
		}
		const T& back() const
		{
			return *(--end());
		}
	private:
		Node* phead; //哨兵位节点（头节点）
		size_t _size; //记录链表长度
	};
	template<class T>
	void swap(T& left, T& right)
	{
		T tmp(left);
		left = right;
		right = tmp;
	}
	template<class T>
	void swap(list<T>& left, list<T>& right)
	{
		left.swap(right);
	}
	template <class Container>
	void printContainer(const Container& container)
	{
		cout << "[";
		bool vis = true;
		for (const auto& e : container)
		{
			if (!vis) {
				cout << ", ";
			}
			cout << e;
			vis = false;
		}
		cout << "]" << endl;
	}
}